package com.aos.dao;


import com.aos.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by quxiaoyan
 */

public interface UserDao extends JpaRepository<User, Long> {

	User findByUsername(String username);

	/**
	 *
	 * @Description:校验用户名是否存在
	 * @param: @param id
	 * @throws
	 */
	User findByUid(Long id);

	/**
	 *
	 * @Description:校验用户名是否存在
	 * @param: @param username
	 * @param: @param serviceid
	 * @throws
	 */
	User findByUsernameAndServiceid(String username,String serviceid);

	/**
	 *
	 * @Description:校验用户名/密码
	 * @param: @param username
	 * @param: @param password
	 * @param: @param serviceid
	 * @throws
	 */
	User findByUsernameAndPasswordAndServiceid(String username,String password,String serviceid);

	/**
	 *
	 * @Description:查询uid最大值
	 *
	 * @throws
	 */
	@Transactional
	@Query(value ="select max(uid) as uid from user where 1=1",nativeQuery = true)
	long getUserByMaxUid();

	/**
	 *
	 * @Description:新增用户信息
	 *
	 */
	@Transactional
	@Modifying(clearAutomatically = true)
	@Query(value = "insert into user (uid, username, realname, password, serviceid,createtime,salt) values (?1,?2, ?3, ?4, ?5, ?6, ?7)",nativeQuery = true)
	int insertUserByUid(long uid,String username,String realname,String password,String serviceid,String createtime,String salt);

	/**
	 *
	 * @Description:更新用户信息
	 *
	 */
	@Transactional
	@Modifying(clearAutomatically = true)
	@Query(value = "update user set password =?2,realname =?3,updatetime =?4,salt =?5 where uid = ?1",nativeQuery = true)
	int updateUserByUid(long uid,String password,String realname,String updatetime,String salt);


	/**
	 *
	 * @Description:删除用户信息
	 *
	 */
	@Transactional
	@Modifying(clearAutomatically = true)
	@Query(value = "delete from user where uid = ?1",nativeQuery = true)
	int deleteUserByUid(long uid);
}
